package com.softserveinc.fragments;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.softserveinc.R;
import com.softserveinc.fragments.map.BalloonOverlayView;
import com.softserveinc.fragments.map.MovieItemizedOverlay;
import com.softserveinc.fragments.map.MovieItemizedOverlay.OnItemTapListener;
import com.softserveinc.fragments.map.OverlayItemsProvider;

public class CinemaMapFragment extends Fragment {

	private static final String CURRENT_POSITION_MESSAGE = "It's where you are standing!";
	private static final String HOLA = "Hola!";
	private static final int ZOOM_LEVEL = 13;
	private static final String TAG = CinemaMapFragment.class.getSimpleName();

	private LocationManager locationManager;

	private MapView mapView;
	private LocationListener locationListener;
	private MovieItemizedOverlay itemizedOverlay;

	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (isAdded()) {
			setupLocationListener();
		}
	}

	@Override
	public void onActivityCreated(final Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		if (isAdded()) {
			mapView = (MapView) getActivity().findViewById(R.id.mapview);
			mapView.setBuiltInZoomControls(true);

			mapView.setVisibility(View.VISIBLE);

			fillMapWithCinemas();
		}
	}

	private void fillMapWithCinemas() {

		Drawable drawable = getResources().getDrawable(R.drawable.icon_needle);
		BalloonOverlayView mapPopup = new BalloonOverlayView(getActivity());

		mapView.addView(mapPopup);

		itemizedOverlay = new MovieItemizedOverlay(mapPopup, mapView, drawable);

		for (OverlayItem item : OverlayItemsProvider.getCinemaOverlayItems()) {
			itemizedOverlay.addOverlay(item, (OnItemTapListener) getActivity());
		}

		addMapOverlay(itemizedOverlay);
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();

		mapView.setVisibility(View.GONE);
	}

	private void addMapOverlay(final MovieItemizedOverlay itemizedOverlay) {
		List<Overlay> mapOverlays = mapView.getOverlays();
		mapOverlays.add(itemizedOverlay);
	}

	private void setupLocationListener() {
		locationManager = (LocationManager) getActivity().getSystemService(
				Context.LOCATION_SERVICE);

		locationListener = new LocationListener() {
			@Override
			public void onLocationChanged(final Location location) {
				Log.i(TAG, "and here we are!");
				try {
					showLocation(mapView, location);
				} catch (Exception e) {
					Log.e(TAG, e.getMessage());
				}
			}

			@Override
			public void onStatusChanged(final String provider,
					final int status, final Bundle extras) {
			}

			@Override
			public void onProviderEnabled(final String provider) {
			}

			@Override
			public void onProviderDisabled(final String provider) {
			}
		};

		locationManager.requestLocationUpdates(
				LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
	}

	private void showLocation(final MapView mapView, final Location location) {
		Log.i(TAG, "is null: " + (location == null));

		if (location == null) {
			return;
		}

		Log.i(TAG, "location : " + location.toString());

		Double geoLat = location.getLatitude() * 1E6;
		Double geoLng = location.getLongitude() * 1E6;
		final GeoPoint point = new GeoPoint(geoLat.intValue(),
				geoLng.intValue());

		Log.i(TAG, "location : " + location.toString());

		Drawable drawable = getResources().getDrawable(
				R.drawable.icon_my_location);

		BalloonOverlayView mapPopup = new BalloonOverlayView(getActivity());

		mapView.addView(mapPopup);

		MovieItemizedOverlay overlay = new MovieItemizedOverlay(mapPopup,
				mapView, drawable);
		OverlayItem overlayitem = new OverlayItem(point, HOLA,
				CURRENT_POSITION_MESSAGE);
		overlay.addOverlay(overlayitem, new OnItemTapListener() {

			@Override
			public void onItemTap() {
				Toast.makeText(getActivity(), point.toString(),
						Toast.LENGTH_SHORT).show();
			}
		});

		addMapOverlay(overlay);

		mapView.getController().animateTo(point);
		mapView.getController().setZoom(ZOOM_LEVEL);

		locationManager.removeUpdates(locationListener);
	}
}
